var models = require('../mysql/mysql');

/**
 * 获取权限菜单接口
 * url: /sys/menu
 * method: get
 * 参数:  
 */
const menuControl = async (req, res, next) => {
	console.log(req.query);
	const {
		roleId
	} = req.query

	var sql = `select * from sysmenu`;
	let data = await models(sql)
	// console.log(JSON.stringify(data));

	let sysmenudata1 = {
		...data[0],
		children: JSON.parse(data[0].children)
	}

	let sysmenudata2 = {
		...data[1],
		children: JSON.parse(data[1].children)
	}


	let sysmenudata3 = {
		...data[2],
		children: JSON.parse(data[2].children)
	}

	let sysmenudata4 = {
		...data[3],
		children: JSON.parse(data[3].children)
	}

	let sysmenuArr
	if (!roleId) sysmenuArr = data
	if (roleId == 1) sysmenuArr = [sysmenudata1, sysmenudata2, sysmenudata3, sysmenudata4]
	if (roleId == 2) sysmenuArr = [sysmenudata1, sysmenudata2, sysmenudata3]
	if (roleId == 3) sysmenuArr = [sysmenudata1]
	console.log(JSON.stringify(sysmenuArr))

	if (data.length > 0) {
		res.send({
			code: 1,
			sysmenu: sysmenuArr

		})
	} else {
		res.send({
			code: -1,
			info: '权限菜单列表为空!',
		})
	}
}
/**
 * 获取各自权限菜单接口
 * url: /sys/operator_menu
 * method: get
 * 参数:  父菜单id 
 */
const operatorMenuControl = async (req, res, next) => {
	console.log(req.query);
	const {
		id
	} = req.query

	var sql = `select * from operator_sysmenu where id=${id}`;
	let data = await models(sql)
	// console.log(JSON.parse(data[0].menu_list));

	if (data.length > 0) {
		res.send({
			code: 1,
			sysmenu: JSON.parse(data[0].menu_list)

		})
	} else {
		res.send({
			code: -1,
			info: '权限菜单列表为空!',
		})
	}
}
// /**
//  * 添加 删除（修改）父/子菜单 （root）
//  *  post
//  *   url: /sys/add_fathermenu
//  */
const getUpdateOperatorMenu = async (req, res, next) => {
	// 接收参数
	const {
		updatemenu
	} = req.body
	console.log(JSON.stringify(updatemenu));

	// var sql = `select * from sysmenu`;
	var sql = `update operator_sysmenu set menu_list='${JSON.stringify(updatemenu)}' where id="${1}";`

	try {
		await models(sql)
		res.send({
			code: 1,
			info: '修改成功',
		})
	} catch (eror) {
		res.send({
			code: -1,
			info: '修改失败',
		})
	}
}
/**
 * 查询 更新 各自权限菜单接口
 * url: /sys/opmenu
 * method: get
 * 参数:  
 */
const updateOperatorMenuControl = async (req, res, next) => {
	console.log(req.body);
	const {
		arrids,
		id
	} = req.body
	let arr2 = [1, 10, 20, 30]
	arr2 = arrids.split(',')

	var sql1 = `select * from operator_sysmenu where id=${1}`;
	let data1 = await models(sql1)
	let arr1 = JSON.parse(data1[0].menu_list)
	// console.log(arr1);
	if (!data1) return
	let arr3 = arr1.filter((item) => {
		return arr2.some(i => item.id == i)
	})
	// console.log(JSON.stringify(arr3));
	// res.send({
	//     code: 1,
	//     info: '修改成功',
	// })
	var sql2 = `update operator_sysmenu set menu_list='${JSON.stringify(arr3)}' where id=${id};`

	try {
		await models(sql2)
		res.send({
			code: 1,
			info: '修改成功',
		})
	} catch (eror) {
		res.send({
			code: -1,
			info: '修改失败',
		})
	}
}



/**
 * 获取子菜单接口
 * url: /sys/menu
 * method: get
 * 参数:  
 */
const submenuControl = async (req, res, next) => {
	// console.log(req.query);
	const {
		menuId
	} = req.query
	console.log(menuId);

	var sql = `select * from operator_sysmenu where id='${1}'`;
	let data = await models(sql)
	// console.log(data.find(item=>item.id == menuId));

	if (data.length > 0) {
		res.send({
			code: 1,
			submenu: JSON.parse(data[0].menu_list)
		})
	} else {
		res.send({
			code: -1,
			info: '子菜单列表为空!',
		})
	}
}
/**
 * 获取按钮资源接口
 * url: /sys/resource
 * method: get
 * 参数:  id
 */
const sysResourceControl = async (req, res, next) => {
	const {
		id
	} = req.query

	var sql = `select * from sys_resource where id=${id}`;
	let data = await models(sql)

	if (data.length > 0) {
		res.send({
			code: 1,
			resource: data[0].resource

		})
	} else {
		res.send({
			code: -1,
			info: '按钮资源列表为空!',
		})
	}
}
/**
 * 获取所有资源列表接口
 * url: /sys/allresource
 * method: get
 */
const sysAllresourceControl = async (req, res, next) => {
	const {
		id
	} = req.query

	var sql = `select * from sys_allresource`;
	let data = await models(sql)
	console.log(data)

	if (data.length > 0) {
		res.send({
			code: 1,
			resource: data

		})
	} else {
		res.send({
			code: -1,
			info: '资源列表为空!',
		})
	}
}
/**
 * 获取所有角色权限列表接口
 * url: /sys/allrole
 * method: get
 */
const sysAllRoleControl = async (req, res, next) => {

	var sql = `select * from sys_permissions`;
	let data = await models(sql)
	console.log(data)

	if (data.length > 0) {
		res.send({
			code: 1,
			info: data

		})
	} else {
		res.send({
			code: -1,
			info: '角色权限列表为空!',
		})
	}
}
/**
 * 获取编辑角色权限列表接口
 * url: /sys/role_update
 * method: post
 */
const sysRolUpdateControl = async (req, res, next) => {
	const {
		id,
		role,
		description
	} = req.body
	console.log(req.body);
	var sql = `update sys_permissions set role="${role}",description="${description}" where id='${id}'`;
	let data = await models(sql)

	try {
		await models(sql)
		res.send({
			code: 1,
			info: '修改成功',
		})
	} catch (eror) {
		res.send({
			code: -1,
			info: '修改失败',
		})
	}
}


/**
 * 修改按钮资源接口
 * url: /sys/menu
 * method: post
 * 参数:  ids
 */
const sysResourceUpdateControl = async (req, res, next) => {
	const {
		arrids,
		id
	} = req.body
	let arr2 = [1, 2, 3]
	arr2 = arrids.split(',')

	var sql1 = `select * from sys_allresource`;
	let data1 = await models(sql1)
	let arr1 = data1
	console.log(arr1);
	if (!data1) return
	let arr3 = arr1.filter((item) => {
		return arr2.some(i => item.id == i)
	})
	console.log(JSON.parse(JSON.stringify(arr3)));
	let arr4 = JSON.parse(JSON.stringify(arr3)).map(i => i.path)
	// console.log(arr4);
	// res.send({
	//     code: 1,
	//     info: '修改成功',
	// })
	var sql2 = `update sys_resource set resource='${JSON.stringify(arr4)}' where id=${id};`

	try {
		await models(sql2)
		res.send({
			code: 1,
			info: '修改成功',
		})
	} catch (eror) {
		res.send({
			code: -1,
			info: '修改失败',
		})
	}
}



module.exports = {
	menuControl,
	submenuControl,
	operatorMenuControl,
	updateOperatorMenuControl,
	sysResourceControl,
	sysAllresourceControl,
	sysResourceUpdateControl,
	getUpdateOperatorMenu,
	sysAllRoleControl,
	sysRolUpdateControl
}